home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2001 May / macformat_103_may_2001.iso / Mac OS X Shareware / Fizilla / Chrome / toolkit.jar / content / global / commonDialog.js < prev    next >
Encoding:
JavaScript  |  2001-03-26  |  8.2 KB  |  259 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is Mozilla Communicator client code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape Communications
  16.  * Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Alec Flett  <alecf@netscape.com>
  22.  *  Ben Goodger <ben@netscape.com>
  23.  *  Blake Ross  <blakeross@telocity.com>
  24.  */
  25.  
  26. var gCommonDialogParam;
  27.  
  28. function commonDialogOnLoad()
  29. {
  30.   doSetOKCancel(commonDialogOnOK, commonDialogOnCancel, commonDialogOnButton2, commonDialogOnButton3);
  31.   gCommonDialogParam = window.arguments[0].QueryInterface(Components.interfaces.nsIDialogParamBlock);
  32.  
  33.   // display the main text
  34.   var messageText = gCommonDialogParam.GetString(0);
  35.   var messageParent = document.getElementById("info.box");
  36.   var messageParagraphs = messageText.split("\n");
  37.   var i;
  38.  
  39.   for (i = 0; i < messageParagraphs.length; i++) {
  40.     var htmlNode = document.createElement("html");
  41.     //htmlNode.setAttribute("style", "max-width: 45em;");
  42.     var text = document.createTextNode(messageParagraphs[i]);
  43.     htmlNode.appendChild(text);
  44.     messageParent.appendChild(htmlNode);
  45.   }
  46.  
  47.   setElementText("info.header", gCommonDialogParam.GetString(3), true);
  48.  
  49.   // set the window title
  50.   window.title = gCommonDialogParam.GetString(12);
  51.  
  52.   // set the icon
  53.   var iconElement = document.getElementById("info.icon");
  54.   var iconURL = gCommonDialogParam.GetString(2);
  55.   if (iconURL)
  56.     iconElement.setAttribute("src", iconURL);
  57.  
  58.   // set the number of command buttons
  59.   var nButtons = gCommonDialogParam.GetInt(2);
  60.   if (nButtons == 1) hideElementById("cancel");
  61.   switch (nButtons) {
  62.     case 4:
  63.       unHideElementByID("Button3");
  64.       document.getElementById("Button3").label = gCommonDialogParam.GetString(11);
  65.       // fall through
  66.     case 3:
  67.       unHideElementByID("Button2");
  68.       document.getElementById("Button2").label = gCommonDialogParam.GetString(10);
  69.       // fall through
  70.     default:
  71.     case 2:
  72.       var string = gCommonDialogParam.GetString(8);
  73.       if (string)
  74.         document.getElementById("ok").label = string;
  75.       // fall through
  76.     case 1:
  77.       string = gCommonDialogParam.GetString(9);
  78.       if (string)
  79.         document.getElementById("cancel").label = string;
  80.       break;
  81.   }
  82.  
  83.   // initialize the checkbox
  84.   setCheckbox(gCommonDialogParam.GetString(1), gCommonDialogParam.GetInt(1));
  85.  
  86.   // initialize the edit fields
  87.   var nEditFields = gCommonDialogParam.GetInt(3);
  88.  
  89.   switch (nEditFields) {
  90.     case 2:
  91.       var containerID, fieldID, labelID;
  92.  
  93.       if (gCommonDialogParam.GetInt(4) == 1) {
  94.         // two password fields ('password' and 'retype password')
  95.         var password2Container = document.getElementById("password2EditField");
  96.         password2Container.removeAttribute("collapsed");
  97.         var password2Field = document.getElementById("dialog.password2");
  98.         password2Field.value = gCommonDialogParam.GetString(7);
  99.  
  100.         var password2Label = gCommonDialogParam.GetString(5);
  101.         if (password2Label)
  102.           setElementText("password2.text", password2Label);
  103.  
  104.         containerID = "password1EditField";
  105.         fieldID = "dialog.password1";
  106.         labelID = "password1.text";
  107.       }
  108.       else {
  109.         // one login field and one password field
  110.         var passwordContainer = document.getElementById("password1EditField");
  111.         passwordContainer.removeAttribute("collapsed");
  112.         var passwordField = document.getElementById("dialog.password1");
  113.         passwordField.value = gCommonDialogParam.GetString(7);
  114.  
  115.         var passwordLabel = gCommonDialogParam.GetString(5);
  116.         if (passwordLabel)
  117.           setElementText("password1.text", passwordLabel);
  118.  
  119.         containerID = "loginEditField";
  120.         fieldID = "dialog.loginname";
  121.         labelID = "login.text";
  122.       }
  123.  
  124.       unHideElementByID(containerID);
  125.       var field = document.getElementById(fieldID);
  126.       field.value = gCommonDialogParam.GetString(6);
  127.  
  128.       var label = gCommonDialogParam.GetString(4);
  129.       if (label)
  130.         setElementText(labelID, label);
  131.       field.focus();
  132.       break;
  133.     case 1:
  134.       var editFieldIsPassword = gCommonDialogParam.GetInt(4);
  135.       if (editFieldIsPassword == 1) {
  136.         containerID = "password1EditField";
  137.         fieldID = "dialog.password1";
  138.         setElementText("password1.text", ""); // hide the meaningless text
  139.       }
  140.       else {
  141.         containerID = "loginEditField";
  142.         fieldID = "dialog.loginname";
  143.         setElementText("login.text", gCommonDialogParam.GetString(4));
  144.       }
  145.  
  146.       unHideElementByID(containerID);
  147.       field = document.getElementById(fieldID);
  148.       field.value = gCommonDialogParam.GetString(6);
  149.       field.focus();
  150.       break;
  151.   }
  152.  
  153.   // set the pressed button to cancel to handle the case where the close box is pressed
  154.   gCommonDialogParam.SetInt(0, 1);
  155.  
  156.   // set default focus
  157.   // preferred order is textbox1, textbox2, textbox 3, OK, cancel, button2, button3
  158.   var visibilityList = ["loginEditField", "password1EditField", "password2EditField", "ok",
  159.                          "cancel", "Button2", "Button3"]
  160.   var focusList      = ["dialog.loginname", "dialog.password1", "dialog.password2", "ok",
  161.                          "cancel", "Button2", "Button3"]
  162.  
  163.   for (i = 0; i < visibilityList.length; i++) {
  164.     if (isVisible(visibilityList[i])) {
  165.       document.getElementById(focusList[i]).focus();
  166.       break;
  167.     }
  168.   }
  169. }
  170.  
  171. function setCheckbox(aChkMsg, aChkValue)
  172. {
  173.   if (aChkMsg) {
  174.     var checkboxElement = document.getElementById("checkbox");
  175.     unHideElementByID("checkboxContainer");
  176.     checkboxElement.label = aChkMsg;
  177.     checkboxElement.checked = aChkValue > 0 ? true : false;
  178.   }
  179. }
  180.  
  181. function unHideElementByID(aElementID)
  182. {
  183.   var element = document.getElementById(aElementID);
  184.   element.removeAttribute("collapsed");
  185. }
  186.  
  187. function hideElementById(aElementID)
  188. {
  189.   var element = document.getElementById(aElementID)
  190.   element.setAttribute("collapsed", "true");
  191. }
  192.  
  193. function isVisible(aElementId)
  194. {
  195.   var element = document.getElementById(aElementId);
  196.   if (element) {
  197.     // XXX check for display:none when getComputedStyle() works properly
  198.     return !(element.getAttribute("collapsed") || element.getAttribute("hidden"));
  199.   }
  200.   return false;   // doesn't exist, so not visible
  201. }
  202.  
  203. function onCheckboxClick(aCheckboxElement)
  204. {
  205.   gCommonDialogParam.SetInt(1, aCheckboxElement.checked);
  206. }
  207.  
  208. function setElementText(aElementID, aValue, aChildNodeFlag)
  209. {
  210.   var element = document.getElementById(aElementID);
  211.   if (!aChildNodeFlag && element)
  212.     element.setAttribute("value", aValue);
  213.   else if (aChildNodeFlag && element)
  214.     element.appendChild(document.createTextNode(aValue));
  215. }
  216.  
  217. function commonDialogOnOK()
  218. {
  219.   gCommonDialogParam.SetInt(0, 0);
  220.   var numEditfields = gCommonDialogParam.GetInt(3);
  221.   var editfield1Password = gCommonDialogParam.GetInt(4);
  222.   if (numEditfields == 2) {
  223.     var editField2;
  224.     if (editfield1Password == 1)
  225.       {
  226.         // we had two password fields
  227.         editField2 = document.getElementById("dialog.password2");
  228.       }
  229.     else
  230.       {
  231.         // we only had one password field (and one login field)
  232.         editField2 = document.getElementById("dialog.password1");
  233.       }
  234.     gCommonDialogParam.SetString(7, editField2.value);
  235.   }
  236.   var editField1 = editfield1Password == 1 ? document.getElementById("dialog.password1") :
  237.                                              document.getElementById("dialog.loginname");
  238.   gCommonDialogParam.SetString(6, editField1.value);
  239.   return true;
  240. }
  241.  
  242. function commonDialogOnCancel()
  243. {
  244.   gCommonDialogParam.SetInt(0, 1);
  245.   return true;
  246. }
  247.  
  248. function commonDialogOnButton2()
  249. {
  250.   gCommonDialogParam.SetInt(0, 2);
  251.   return true;
  252. }
  253.  
  254. function commonDialogOnButton3()
  255. {
  256.   gCommonDialogParam.SetInt(0, 3);
  257.   return true;
  258. }
  259.